﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class RepairBoxes : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (!IsPostBack)
            {
                ReloadBoxList();
            }

            btnAddBox.Enabled = Branches.CurrentBranchID == SessionUser.BranchID && SessionUser.Admin;
            btnEditBox.Enabled = Branches.CurrentBranchID == SessionUser.BranchID && SessionUser.Admin;
            btnDeleteBox.Enabled = Branches.CurrentBranchID == SessionUser.BranchID && SessionUser.Admin;

            if (Branches.CurrentBranchID != SessionUser.BranchID ||  !SessionUser.Admin)
            {
                btnAddBox.OnClientClick = "";
                btnEditBox.OnClientClick = "";
                btnDeleteBox.OnClientClick = "";
            }
        }

        private void ReloadBoxList()
        {
            View_RepairBoxes boxes = View_RepairBoxes.GetAllActiveBoxes();

            lstRepairBox.BoxList = boxes;
            lstRepairBox.Refresh();
        }

        protected void btnAddBox_OnClick(object sender, EventArgs e)
        {
            ReloadBoxList();
        }

        protected void btnRemoveBox_OnClick(object sender, EventArgs e)
        {
            try
            {
                lstRepairBox.DeleteCheckedItems();
                lstRepairBox.Refresh();
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairBoxes.aspx.cs", "btnRemoveBox_OnClick", ex.StackTrace);
                ((Popup)this.Master).ShowErrorMessage("Error: " + ex.Message);
            }
        }

        public string GetBoxEditPageLink()
        {
            return this.ResolveUrl("~/EditPages/EditRepairBox.aspx?");
        }
    }
}